﻿using System;

namespace GeometryHomework
{
    // 抽象基类（核心校验逻辑）
    public abstract class Shape
    {
        public abstract bool IsValid { get; }
        public abstract double CalculateArea();
    }

    // 长方形（含输入校验）
    public class Rectangle : Shape
    {
        public double Length { get; }
        public double Width { get; }

        public Rectangle(double l, double w)
        {
            if (l <= 0 || w <= 0) throw new ArgumentException("边长必须>0");
            Length = l;
            Width = w;
        }

        public override bool IsValid => true;
        public override double CalculateArea() => Length * Width;
    }

    // 正方形（继承复用）
    public class Square : Rectangle
    {
        public Square(double side) : base(side, side) { }
    }

    // 三角形（海伦公式实现）
    public class Triangle : Shape
    {
        public double A { get; }
        public double B { get; }
        public double C { get; }

        public Triangle(double a, double b, double c)
        {
            if (a <= 0 || b <= 0 || c <= 0) throw new ArgumentException("边长必须>0");
            A = a;
            B = b;
            C = c;
        }

        public override bool IsValid => (A + B > C) && (B + C > A) && (C + A > B);

        public override double CalculateArea()
        {
            double p = (A + B + C) / 2;
            return Math.Sqrt(p * (p - A) * (p - B) * (p - C));
        }
    }

    class Program
    {
        static void Main()
        {
            // 午时特供计算案例 
            try
            {
                var rect = new Rectangle(5, 3);
                Console.WriteLine($"长方形面积：{rect.CalculateArea()}");

                var square = new Square(4);
                Console.WriteLine($"正方形面积：{square.CalculateArea()}");

                var triangle = new Triangle(3, 4, 5);
                Console.WriteLine($"三角形面积：{triangle.CalculateArea():F2}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"错误：{ex.Message}");
            }

            // 防闪退机制 
            Console.Write("\n按任意键退出...");
            Console.ReadKey();
        }
    }
}